import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:provider/provider.dart';

import '../../../data/models/bt_block.dart';
import '../../../widgets/pagination/bt_first_page_error_indicator.dart';
import '../../../widgets/pagination/bt_first_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_new_page_error_indicator.dart';
import '../../../widgets/pagination/bt_new_page_progress_indicator.dart';
import '../../../widgets/pagination/bt_no_items_found_indicator.dart';
import '../../../widgets/pagination/bt_no_more_items_indicator.dart';
import '../../utils/bt_app_bar_utils.dart';
import '../models/bt_block_list_model.dart';
import '../widgets/bt_block_item_widget.dart';

@RoutePage(name: "blockListRoute")
class BtBlockListScreen extends StatelessWidget {
  const BtBlockListScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFFFFFFF),
      appBar: appBar(context, 'Block List', null),
      body: Container(
        margin: const EdgeInsets.only(top: 12),
        child: ChangeNotifierProvider(
          create: (context) => BtBlockListModel(),
          child: Consumer<BtBlockListModel>(builder: (context, model, _) {
            return RefreshIndicator(
              onRefresh: () => Future.sync(
                () => model.pagingController.refresh(),
              ),
              child: MediaQuery.removePadding(
                context: context,
                removeTop: true,
                child: PagedListView<int, BtBlock>(
                  pagingController: model.pagingController,
                  builderDelegate: PagedChildBuilderDelegate<BtBlock>(
                    itemBuilder: (context, item, index) {
                      return BtBlockItemWidget(
                        index: index,
                        item: item,
                        model: model,
                      );
                    },
                    firstPageErrorIndicatorBuilder: (_) => BtFirstPageErrorIndicator(
                      error: model.pagingController.error,
                      onTryAgain: () => model.pagingController.refresh(),
                    ),
                    newPageErrorIndicatorBuilder: (_) => BtNewPageErrorIndicator(
                      error: model.pagingController.error,
                      onTap: () => model.pagingController.retryLastFailedRequest(),
                    ),
                    firstPageProgressIndicatorBuilder: (_) => const BtFirstPageProgressIndicator(),
                    newPageProgressIndicatorBuilder: (_) => const Padding(
                      padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                      child: BtNewPageProgressIndicator(),
                    ),
                    noItemsFoundIndicatorBuilder: (_) => const Padding(
                      padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                      child: BtNoItemsFoundIndicator(),
                    ),
                    noMoreItemsIndicatorBuilder: (_) => const Padding(
                      padding: EdgeInsets.fromLTRB(0, 0, 0, 10),
                      child: BtNoMoreItemsIndicator(),
                    ),
                  ),
                ),
              ),
            );
          }),
        ),
      ),
    );
  }
}
